package com.qigao.mall.service.impl;

import com.qigao.mall.api.ShopInfoService;
import com.qigao.mall.commons.dto.ShopInfoCreateDto;
import com.qigao.mall.commons.dto.ShopInfoDto;
import com.qigao.mall.commons.enums.IdTypeEnum;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.enums.StatusEnum;
import com.qigao.mall.commons.exceptions.BusinessException;
import com.qigao.mall.commons.utils.DateUtil;
import com.qigao.mall.commons.utils.IDUtils;
import com.qigao.mall.jdbc.mapper.TbShopInfoMapper;
import com.qigao.mall.jdbc.model.TbShopInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ShopInfoServiceImpl implements ShopInfoService {
    private static final Logger logger= LoggerFactory.getLogger(ShopInfoServiceImpl.class);
    @Resource
    private TbShopInfoMapper tbShopInfoMapper;
    @Override
    public TbShopInfo create(ShopInfoCreateDto shopInfoCreateDto) {
        TbShopInfo oldShopInfo=tbShopInfoMapper.selectByShopName(shopInfoCreateDto.getShopName());
        if (oldShopInfo!=null){
            logger.warn("店铺信息已存在:{}",shopInfoCreateDto.getShopName());
            throw new BusinessException(ResultCode.SHOP_HAS_EXITS);
        }
        TbShopInfo tbShopInfo=new  TbShopInfo();
        BeanUtils.copyProperties(shopInfoCreateDto,tbShopInfo);
        tbShopInfo.setShopNo(IDUtils.genIdStr(IdTypeEnum.SHOP));
        tbShopInfo.setCreateNo(shopInfoCreateDto.getOperNo());
        tbShopInfo.setCreateTime(new Date());
        tbShopInfo.setModiNo(shopInfoCreateDto.getOperNo());
        tbShopInfo.setModiTime(new Date());
        tbShopInfoMapper.insertSelective(tbShopInfo);
        return tbShopInfo;
    }

    @Override
    public void update(ShopInfoCreateDto shopInfoCreateDto) {
        TbShopInfo tbShopInfo=new  TbShopInfo();
        BeanUtils.copyProperties(shopInfoCreateDto,tbShopInfo);
        tbShopInfo.setCreateNo(shopInfoCreateDto.getOperNo());
        tbShopInfo.setCreateTime(new Date());
        tbShopInfo.setModiNo(shopInfoCreateDto.getOperNo());
        tbShopInfo.setModiTime(new Date());
        tbShopInfoMapper.updateByPrimaryKeySelective(tbShopInfo);
    }

    @Override
    public void updateByShopNo(ShopInfoCreateDto shopInfoCreateDto) {
        TbShopInfo tbShopInfo=new  TbShopInfo();
        BeanUtils.copyProperties(shopInfoCreateDto,tbShopInfo);
        tbShopInfo.setCreateNo(shopInfoCreateDto.getOperNo());
        tbShopInfo.setCreateTime(new Date());
        tbShopInfo.setModiNo(shopInfoCreateDto.getOperNo());
        tbShopInfo.setModiTime(new Date());
        tbShopInfoMapper.updateByShopNo(tbShopInfo);
    }

    @Override
    public ShopInfoDto findByOrderNo(String orderNo) {
        TbShopInfo tbShopInfo= tbShopInfoMapper.selectByOrderNo(orderNo);
        return convertDto(tbShopInfo);
    }

    @Override
    public ShopInfoDto findByShopName(String shopName) {
        TbShopInfo oldShopInfo=tbShopInfoMapper.selectByShopName(shopName);
        return convertDto(oldShopInfo);

    }



    private ShopInfoDto convertDto(TbShopInfo oldShopInfo) {
        if(oldShopInfo==null) return null;
        ShopInfoDto shopInfoDto =new ShopInfoDto();
        BeanUtils.copyProperties(oldShopInfo, shopInfoDto);
        shopInfoDto.setValidBeginTime(DateUtil.getTimeFormat(oldShopInfo.getValidBeginTime()));
        shopInfoDto.setValidEndTime(DateUtil.getTimeFormat(oldShopInfo.getValidEndTime()));
        shopInfoDto.setValidStatusName(StatusEnum.getNameByCode(shopInfoDto.getValidStatus()));
        return shopInfoDto;
    }

    @Override
    public void enableDisable(Long id, String currentUserNo) {
        TbShopInfo tbShopInfo=new  TbShopInfo();
        tbShopInfo.setId(id);
        tbShopInfo.setModiNo(currentUserNo);
        tbShopInfo.setModiTime(new Date());
        tbShopInfoMapper.enableDisable(tbShopInfo);
    }



    @Override
    public ShopInfoDto findByShopNo(String shopNo) {
        TbShopInfo tbShopInfo= tbShopInfoMapper.selectByShopNo(shopNo);
        return convertDto(tbShopInfo);
    }

    @Override
    public List<ShopInfoDto> findListByMerNo(String merNo) {
        List<TbShopInfo> tbShopInfos= tbShopInfoMapper.selectByMerNo(merNo);
        List<ShopInfoDto> shopInfoDtos =new ArrayList<ShopInfoDto>();
        if(tbShopInfos!=null && tbShopInfos.size()>=1){
            for(TbShopInfo tbShopInfo:tbShopInfos){
                ShopInfoDto shopInfoDto =convertDto(tbShopInfo);
                shopInfoDtos.add(shopInfoDto);
            }
        }
        return shopInfoDtos;
    }


}
